package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.rpc.api.AnalysisBlackListRpcService;
import com.hunttown.mes.rpc.domain.AnalysisBlackListDTO;
import com.hunttown.mes.rpc.domain.query.AnalysisBlackListDTOQuery;
import com.hunttown.mes.service.AnalysisBlackListService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.AnalysisBlackListConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 15:03:56
 */
@Service
public class AnalysisBlackListRpcServiceImpl implements AnalysisBlackListRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisBlackListRpcServiceImpl.class);

    private final AnalysisBlackListService analysisBlackListService;

    @Autowired
    public AnalysisBlackListRpcServiceImpl(AnalysisBlackListService analysisBlackListService) {
        this.analysisBlackListService = analysisBlackListService;
    }

    @Override
    public AnalysisBlackListDTO insert(AnalysisBlackListDTO analysisBlackListDTO) {
        try {
            return convert(analysisBlackListService.insert(convert(analysisBlackListDTO)));
        } catch (Exception e) {
            logger.error("AnalysisBlackList create failed.", e);
            throw new RuntimeException("AnalysisBlackList create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisBlackListDTO analysisBlackListDTO) {
        try {
            return analysisBlackListService.updateInfoById(convert(analysisBlackListDTO));
        } catch (Exception e) {
            logger.error("AnalysisBlackList update by id failed.", e);
            throw new RuntimeException("AnalysisBlackList update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisBlackListDTOQuery analysisBlackListDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisBlackListDTOQuery.getW_id() != null && analysisBlackListDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisBlackListDTOQuery.getW_id());
            }

            if (analysisBlackListDTOQuery.getIds() != null && !analysisBlackListDTOQuery.getIds().equals("")) {
                query.put("ids", analysisBlackListDTOQuery.getIds());
            }

            if (analysisBlackListDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", analysisBlackListDTOQuery.getStopFlag());
            }

            if (analysisBlackListDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisBlackListDTOQuery.getDeleteFlag());
            }

            return analysisBlackListService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisBlackList update by query failed.", e);
            throw new RuntimeException("AnalysisBlackList update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisBlackListDTO selectById(Integer id) {
        try {
            return convert(analysisBlackListService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisBlackList select by id failed.", e);
            throw new RuntimeException("AnalysisBlackList select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisBlackListDTO selectObjByQuery(AnalysisBlackListDTOQuery analysisBlackListDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(analysisBlackListService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisBlackList select obj by query failed.", e);
            throw new RuntimeException("AnalysisBlackList select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisBlackListDTO> getListForPage(AnalysisBlackListDTOQuery analysisBlackListDTOQuery) {
        Query query = Query.page(analysisBlackListDTOQuery.getPn(), analysisBlackListDTOQuery.getPageSize());

        if (analysisBlackListDTOQuery.getIds() != null && !analysisBlackListDTOQuery.getIds().equals("")) {
            query.put("ids", analysisBlackListDTOQuery.getIds());
        }

        if (analysisBlackListDTOQuery.getLikeName() != null && !analysisBlackListDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisBlackListDTOQuery.getLikeName());
        }

        if (analysisBlackListDTOQuery.getOrderBy() != null && analysisBlackListDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisBlackListDTOQuery.getOrderBy());
        }

        if (analysisBlackListDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", analysisBlackListDTOQuery.getStopFlag());
        }

        if (analysisBlackListDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", analysisBlackListDTOQuery.getDeleteFlag());
        }

        List<AnalysisBlackListDTO> list = convert(analysisBlackListService.selectByQuery(query));
        Page<AnalysisBlackListDTO> page = Page.from(analysisBlackListDTOQuery.getPn(), analysisBlackListDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisBlackListService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisBlackListService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisBlackList delete failed.", e);
            throw new RuntimeException("AnalysisBlackList delete failed, Please order your code.", e);
        }
    }
}